package com.forlove.app.qselect.friend.presenter;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.forlove.app.common.network.ICallBack;
import com.forlove.app.common.utils.LogUtil;
import com.forlove.app.common.utils.ParseHelper;
import com.forlove.app.common.utils.StringUtils;
import com.forlove.app.manager.DataManager;
import com.forlove.app.manager.NetworkManager;
import com.forlove.app.manager.ServerManager;
import com.forlove.app.qselect.CommonEntity;
import com.forlove.app.qselect.friend.entity.MineFriendEntity;
import com.forlove.app.qselect.friend.entity.SchoolFriendEntity;
import com.forlove.app.qselect.friend.service.ISchoolFriendService;
import com.forlove.app.qselect.friend.service.impl.SchoolFriendServiceImpl;
import com.forlove.app.qselect.friend.view.SchoolFriendView;
import com.forlove.app.qselect.login.entity.SchoolDto;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yjl on 2017/11/27.
 */

public class SchoolFriendPresenter extends MineNewFriendPresenter {
    private ISchoolFriendService schoolFriendService;
    private SchoolFriendView schoolFriendView;
    private List<MineFriendEntity> mineFriendEntities = new ArrayList<>();
    private int page = 1;

    public SchoolFriendPresenter(SchoolFriendView schoolFriendView) {
        super(schoolFriendView);
        this.schoolFriendView = schoolFriendView;
        if (schoolFriendService == null) {
            schoolFriendService = new SchoolFriendServiceImpl();
            setService(schoolFriendService);
        }
    }

    public void loadSchoolInfo() {
        schoolFriendService.loadData(ServerManager.ACTION_GET_SCHOOL, new HashMap(), new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "学校信息:" + result);
                String school = ParseHelper.getString(result, "data");
                if (StringUtils.isNotEmpty(school)) {
                    SchoolDto schoolDto = JSON.parseObject(school, SchoolDto.class);
                    if (schoolDto != null) {
                        DataManager.saveSchoolInfo(school);
                    }
                    schoolFriendView.updateSchoolInfo(schoolDto.getData());
                }
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    schoolFriendView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                } else {
                    schoolFriendView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                }
            }
        });
    }


    public void refreshSchoolFriend(Map param) {
        page = 1;
        loadSchoolFriend(param);
    }

    /**
     * 刷新
     *
     * @param param
     */
    private void loadSchoolFriend(Map param) {
        param.put("page", page);
        schoolFriendService.loadData(ServerManager.ACTION_SCHOOL_FRIEND, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "学校好友");
                SchoolFriendEntity schoolFriendEntity = ParseHelper.getModel(result, "data", SchoolFriendEntity.class);
                if (schoolFriendEntity != null) {
                    if (page == 1) {
                        mineFriendEntities.clear();
                        mineFriendEntities.addAll(schoolFriendEntity.getList());
                    } else {
                        mineFriendEntities.addAll(schoolFriendEntity.getList());
                    }
                    if (schoolFriendEntity.getList().size() == 0) {
                        page--;
                    }
                    schoolFriendView.updateSchoolFriend(mineFriendEntities);
                }
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    schoolFriendView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                } else {
                    schoolFriendView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                }
            }
        });
    }

    public void loadMoreSchoolFriend(Map param) {
        page++;
        loadSchoolFriend(param);
    }
}
